package com.zhifou.controller;

import com.zhifou.annotation.Access;
import com.zhifou.annotation.CurrentUser;
import com.zhifou.annotation.NoRepeatSubmit;
import com.zhifou.common.Result;
import com.zhifou.constants.RedisPrefix;
import com.zhifou.model.User;
import com.zhifou.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author 知否技术
 * @description TODO
 * @date 2023-02-23 9:02
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        // 这里是测试案例，实际要从数据库查出来该用户信息
        user.setId("11112222");
        user.setAge(12);
        String jwtToken = jwtUtil.createJwtToken(user.getId(), 100 * 10);
        redisTemplate.opsForValue().set(RedisPrefix.TOKEN_USER_LOGIN + jwtToken, user, 60 * 10, TimeUnit.SECONDS);
        return Result.success(jwtToken);
    }

    @GetMapping("/list")
    @NoRepeatSubmit(timeout = 10000)
    public Result getUserList(@CurrentUser User user) {
        log.info("---->当前登录的用户：{}", user.getUsername());
        return Result.success(null);
    }

    @GetMapping("/girls")
    @Access()
    public Result getGirls() {
        log.info("---->获取美女信息");
        return Result.success(null);
    }

}
